- Plotting options
- Grammar of graphics
- ggplot2: the basics
- ggplot2: intermediate
- Summary
NAFC | Fisheries and Oceans Canada | 2019-01-16
“The greatest value of a picture is when it forces us to notice what we never expected to see.” — John Tukey
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
duck <- read_excel("data/Duck Islands Movement.xlsx")
round <- read_excel("data/Round Island Movement.xlsx")
tags <- bind_rows(duck, round) %>% # stack both data sets
select(SIDE:NOTCH) %>% # select main columns
drop_na(SIDE, ISLAND, YEAR, TAG) # drop empty rows
ggplot(data = tags, mapping = aes(x = YEAR, y = LENGTH)) + # Data
geom_point() + # Geometry
coord_cartesian() # Coordinates
ggplot function starts the plot
data is supplied firstmapping is specified second using the aes (aesthetics) functiongeom_point adds points to the base layercoord_cartesian defines the coordinate system
Note that the pipe operator (
%>%) is not used here, rather you add items (+)
ggplot(tags, aes(x = YEAR, y = LENGTH)) +
geom_point()
ggplot(tags, aes(x = YEAR, y = LENGTH, group = TAG)) +
geom_line()
ggplot(tags, aes(x = SEX, y = LENGTH)) +
geom_boxplot()
ggplot(tags, aes(x = YEAR, fill = ISLAND)) + geom_bar()
ggplot(tags, aes(x = YEAR, y = LENGTH, group = TAG)) +
geom_line()
Add the colour aesthetic to the above code make the plot below
Move away from defaults to publication quality
p <- ggplot(tags) p + geom_line(aes(x = YEAR, y = LENGTH, group = TAG, colour = SEX))
p + geom_line(aes(x = YEAR, y = LENGTH, group = TAG, color = ISLAND))
p + geom_line(aes(x = YEAR, y = LENGTH, group = TAG, color = SIDE))
p + geom_bar(aes(x = YEAR, fill = ISLAND), stat = "count")
p + geom_bar(aes(x = YEAR)) +
facet_grid(~ ISLAND) # split plots by island
p + geom_bar(aes(x = YEAR)) +
facet_grid(SEX ~ ISLAND) # split plots by sex and island
p + geom_bar(aes(x = YEAR, fill = factor(MONTH))) +
facet_grid(SEX ~ ISLAND)
## Save the first step into an object called p1
p1 <- ggplot(tags) +
geom_bar(aes(x = YEAR, fill = factor(MONTH))) +
facet_grid(SEX ~ ISLAND)
## Here's what the defaults look like p1
p2 <- p1 + xlab("Year") + ylab("Number of records") # change x and y labels
p2
p3 <- p2 + scale_fill_brewer(palette = "RdBu", name = "Month") # name legend and use greyscale p3
p4 <- p3 + coord_cartesian(ylim = c(0, 700), expand = FALSE) # set y limits and remove buffer p4
p5 <- p4 + theme_bw() # use the black and white theme p5
p6 <- p5 + theme(panel.grid.major = element_blank()) # remove major grid lines p6
p7 <- p6 + theme(panel.grid.minor = element_blank()) # remove minor grid lines p7
p8 <- p7 + theme(strip.background = element_blank()) # remove facet strip background p8
## Set-up nice island and sex labels
island_names <- c("duck" = "Duck",
"round" = "Round")
sex_names <- c("F" = "Female",
"M" = "Male")
## Store the plot in an object
p <- ggplot(tags) + # set-up base layer
geom_bar(aes(x = YEAR, fill = factor(MONTH)), stat = "count") + # add bars by year and filled by month
facet_grid(SEX ~ ISLAND, # facet by sex and island
labeller = labeller(SEX = sex_names, # edit facet labels
ISLAND = island_names)) +
xlab("Year") + ylab("Number of records") + # change x and y labels
scale_fill_brewer(palette = "RdBu", name = "Month") + # name legend and use greyscale
coord_cartesian(ylim = c(0, 700), expand = FALSE) + # set y limits and remove buffer
theme_bw() + # use the black and white theme
theme(panel.grid.major = element_blank(), # remove major grid lines
panel.grid.minor = element_blank(), # remove minor grid lines
strip.background = element_blank()) # remove facet strip background
p
ggplot2 and dplyr